package com.blockchain.console.servlets.financial.tx;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.WithdrawStatus;
import com.blockchain.console.service.financial.TxglManage;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;

@Right(id = "C_FINANCIAL_BATCHCHECKTXGL", name = "人民币提现-未审核-批量审核")
public class BatchCheckTxgl extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		TxglManage extraction = serviceSession.getService(TxglManage.class);
		String s = request.getParameter("ids");
		int[] ids = new int[0];
		if (!StringHelper.isEmpty(s)) {
			ids = IntegerParser.parseArray(s.split(","));
		}
		String check_reason = request.getParameter("check_reason");
		WithdrawStatus status = EnumParser.parse(WithdrawStatus.class,
				request.getParameter("status"));
		extraction.checkBatch(status, check_reason, ids);
		if (status == WithdrawStatus.SHSB) {
			sendRedirect(request, response,
					getController().getURI(request, Shsb.class));
			
		} else if (status == WithdrawStatus.SHTG) {
			sendRedirect(request, response,
					getController().getURI(request, Shtg.class));
			
		}
	}

	@Override
	protected void onThrowable(HttpServletRequest request,
			HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		if (throwable instanceof ParameterException
				|| throwable instanceof LogicalException) {
			getController().prompt(request, response, PromptLevel.WARRING,
					throwable.getMessage());
			sendRedirect(request, response,
					getController().getURI(request, TxglList.class));
		} else {
			super.onThrowable(request, response, throwable);
		}
	}
}
